package com.gdpu.project.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.apache.struts2.ServletActionContext;

import com.gdpu.page.vo.PageView;
import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.service.TeacherService;
import com.gdpu.project.vo.News;
import com.opensymphony.xwork2.ActionSupport;

public class NewsAction extends ActionSupport {
	private static final long serialVersionUID = 1L;
	private String newsType;
	private String newsTitle;
	private String newsInfo;
	private TeacherService teacherService;
	private int currentpage=1; 
	private PageView<News> pageView=new PageView<News>(10,currentpage);
	private HttpServletRequest request;
	private HttpSession session;

	public String getNewsType() {
		return newsType;
	}

	public void setNewsType(String newsType) {
		this.newsType = newsType;
	}

	public String getNewsTitle() {
		return newsTitle;
	}

	public void setNewsTitle(String newsTitle) {
		this.newsTitle = newsTitle;
	}

	public String getNewsInfo() {
		return newsInfo;
	}

	public void setNewsInfo(String newsInfo) {
		this.newsInfo = newsInfo;
	}

	public TeacherService getTeacherService() {
		return teacherService;
	}

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public void setPageView(PageView<News> pageView) {
		this.pageView = pageView;
	}

	public void setCurrentpage(int currentpage) {
		this.currentpage = currentpage;
	}

	public PageView<News> getPageView() {
		return pageView;
	}

	/**
	 * 新闻发布Action
	 * @return
	 */
	public String newsAddAction() {
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		News news = new News();
		news.setNewsType(getNewsType());
		news.setNewsTitle(getNewsTitle());
		news.setNewsInfo(getNewsInfo());
		news.setFbTime(new Date());
		news.setClickRate(0);
		Boolean flag = teacherService.addNews(news);
		if(flag){
			List<News> newslist = teacherService.findAllNews();
			request.setAttribute("newslist", newslist);
			return "success";
		}else{
			request.setAttribute("message", "发布失败，请重试！");
			return "failed";
		}
	}
	
	/**
	 * 新闻修改Action
	 * @return
	 */
	public String newsUpdateAction(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		int id = Integer.parseInt(request.getParameter("newsId").toString());
		News news =teacherService.findNewsById(id);
		news.setNewsType(getNewsType());
		news.setNewsTitle(getNewsTitle());
		news.setNewsInfo(getNewsInfo());
		news.setClickRate(0);
		news.setFbTime(new Date());
		Boolean flag = teacherService.updateNews(news);
		
		if (flag) {
			List<News> newslist = teacherService.findAllNews();
			request.setAttribute("newslist", newslist);
			request.setAttribute("message", "更新成功！");
			return "success";
		} else {
			request.setAttribute("news", news);
			request.setAttribute("message", "更新失败，请重试！");
			return "failed";
		}	
	}
	
	/**
	 * 删除新闻Action
	 * @return
	 */
	public String deleteNews(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		int id = Integer.parseInt(request.getParameter("newsId").toString());
		Boolean flag = teacherService.deleteNewsById(id);
		
		if(flag){
			List<News> newslist = teacherService.findAllNews();
			request.setAttribute("newslist", newslist);
			request.setAttribute("message", "新闻删除成功！");
			return "success";
		}else{
			request.setAttribute("message", "删除失败，请重试！");
			return "failed";
		}

	}
	
	public String listAllNews(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		QueryResult<News> queryResult = teacherService.findAllNews(pageView.getFirstResult(), pageView.getMaxresult());
		pageView.setQueryResult(queryResult);
		request.setAttribute("pageView", pageView);
		request.setAttribute("newslist",queryResult.getResultlist());
		return "success";
	}
	
	public String findNewsById(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		int newsId =Integer.parseInt(request.getParameter("newsId").toString());
		News news = teacherService.findNewsById(newsId);
		request.setAttribute("news",news);
		return "success";
	}

	public String lookNewsById(){
		request = ServletActionContext.getRequest();
		session = request.getSession();
		session.getServletContext();
		int newsId =Integer.parseInt(request.getParameter("newsId").toString());
		News news = teacherService.findNewsById(newsId);
		teacherService.updateNews(news);
		request.setAttribute("news",news);
		return "success";
		
	}

}
